<?php
/**
 * tpshop
 * ============================================================================
 * 版权所有 2016-2026 欣才开裆裤开发组
 * 网站地址: http://git.oschina.net/ts128
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和使用 。
 * 不允许对程序代码以任何形式任何目的的再发布。
 * ============================================================================
 * Author: 人码
 * Date: 2016-10-15
 */
namespace Admin\Controller;
use Admin\Controller\BaseController;
/**
*	酒店模块
*/
class HotelsController extends BaseController {
	private $perpage = 10;
    private $M;
    public function _initialize(){
        $this->M = M("Hotels");
    }
/*
*   分页
*/
    public function index(){
        $ada = M("area");
        $data = $ada->create();
        if(empty($data["id"])){
            $res = $ada->add();
        }
        else{
            $res = $ada->save();
        }
        $count = $this->M->count();    // 总记录数
        $pages = ceil($count / $this->perpage);
        if(empty($nowpage)){
            $nowpage = 1;
        }
        $Page = new \Think\Page($count, $this->perpage);
        $Page->setConfig("prev", "上一页");
        $Page->setConfig("next", "下一页");
        $this->M->alias("h")->join(array(
            "left JOIN y_area a on a.id=h.province",
            "left join y_area b on b.id=h.city",
            "LEFT JOIN y_area c on c.id=h.district",
            "left join __SENIC__ s on s.id=h.senic_id"
        ))->field(array(
            "h.*",
            "a.name"=> "province",
            "b.name"=> "city",
            "c.name"=> "district",
            "s.name"=> "senic"
        ));
        $list = $this->M->limit($Page->firstRow.','.$Page->listRows)->select();   // 每页的数据数组
        $show = $Page->show();  // 页码链接内容
        $this->assign("count", $count);
        $this->assign("list", $list);
        $this->assign("page", $show);
        $this->assign("pages", $pages);
        $this->assign("nowpage", $nowpage);
        //提取出遍历所有的子节点
        session_start();
        $id=I("get.");
        $id=$id["id"];
        $_SESSION["pid"]=$id;
        $id=$_SESSION["pid"];
        $M=M("admin_node");
        $node=$M->where("parent_id='$id'")->select();
        $_SESSION["node"]=$node;
        $this->display();
    }
/**
*   添加/修改酒店信息
*/
    public function edit(){
        $id = I("id");
        if(!empty($id)){
            $data = $this->M->where("id=".$id)->select();
            $this->assign("data", $data[0]);
        }
        $senic = M("senic")->select();
        $this->assign("senic", $senic);
        $this->display();
    }
/**
*	添加/修改酒店信息处理过程
*/
    public function doEdit(){
        $data = $this->M->create();
        if(empty($data["id"])){
            $res = $this->M->data($data)->add();
            // 初次添加数据时上传图片
            $picres = $this->upPhoto($res);
        }
        else{
            $res = $this->M->data($data)->save();
            // 更新信息时上传图片
            $picres = $this->upPhoto($data["id"]);
        }
        if($res || $picres){
            $this->success("酒店信息已被修改，".$picres, U("Hotels/index"));
        }
        else{
            $this->error("操作失败", U("Hotels/index"));
        }
    }
/*
*   酒店图片上传
*/
    public function upPhoto($id = ""){
        if(empty($id)){
            return ;
        }
        // 图片上传
        $upload = new \Think\Upload();// 实例化上传类
        $upload->maxSize   =     3145728 ;// 设置附件上传大小
        $upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
        $upload->savePath  =     CONTROLLER_NAME.'/'; // 设置附件上传目录    // 上传文件
        $upload->subName = array('date','Y/m');
        $info = $upload->upload();
        if(!$info) {// 上传错误提示错误信息
            return $upload->getError();
        }else{// 上传成功
            $pic = M("hotels_pic");
            $picture = array();
            foreach($info as $v){
                $picture = substr($v["savepath"], strlen(CONTROLLER_NAME)+1) . $v["savename"];
                $pic->data(array("hotels_id"=>$id, "picture"=>$picture))->add();
            }
            return count($info)."个图片已上传";
        }
    }
/*
*   ajax请求查询酒店图片
*/
    public function getPic(){
        $id = I("id");
        $picinfo["pic"] = M("hotels_pic")->where("hotels_id=".$id)->select();
        $picinfo["pics"] = M("hotels_pic")->where("hotels_id=".$id)->count();
        echo json_encode($picinfo);
    }
/*
*   ajax请求删除酒店图片
*/
    public function delPic(){
        $id = I("id");
        $picture = $this->M->table("__HOTELS_PIC__")->where("id=".$id)->select();
        $delres = unlink(C("TMPL_PARSE_STRING")["__UPLOAD__"]."/".CONTROLLER_NAME."/".$picture[0]["picture"]);
        if($delres){
            $res = $this->M->table("__HOTELS_PIC__")->where("id=".$id)->delete();
        }
        echo $res;
    }
/**
*   删除酒店信息
*/
    public function del(){
        $id = I("id");
        $pic = M("hotels_pic");
        $res1 = $this->M->where("id=".$id)->delete();
        $data = $pic->where("hotels_id=".$id)->select();
        foreach($data as $v){
            unlink(C("TMPL_PARSE_STRING")["__UPLOAD__"]."/".CONTROLLER_NAME."/".$v["picture"]);
        }
        $res2 = $pic->where("hotels_id=".$id)->delete();
        if($res1){
            $this->success("操作成功", U("Hotels/index"));
        }
        else{
            $this->error("操作失败", U("Hotels/index"));
        }
    }

// ----------------分--------割--------线--------小--------盆--------友----------------

/**
*   显示客房列表
*/
    public function room(){
        $count = M("rooms")->count();    // 总记录数
        $pages = ceil($count / $this->perpage);
        if(empty($nowpage)){
            $nowpage = 1;
        }
        $Page = new \Think\Page($count, $this->perpage);
        $Page->setConfig("prev", "上一页");
        $Page->setConfig("next", "下一页");
        $aaa=M("rooms");
        $aaa->alias("r")->join(array(
            "LEFT JOIN y_hotels h on r.hotels_id=h.id",
            "LEFT JOIN y_rooms_type t on r.type=t.id"
        ))->field(array(
            "r.*",
            "h.name"=> "hotel",
            "t.name"=> "name"
        ));
        $list = $aaa->limit($Page->firstRow.','.$Page->listRows)->select();   // 每页的数据数组
        $show = $Page->show();  // 页码链接内容
        $this->assign("count", $count);
        $this->assign("list", $list);
        $this->assign("page", $show);
        $this->assign("pages", $pages);
        $this->assign("nowpage", $nowpage);
        $this->display();
    }
/**
*   编辑客房信息
*/
    public function editRoom(){
        $id = I("id");
        if(!empty($id)){
            $data = M("rooms")->where("id=".$id)->select();
            $hotels_id = $data[0]["hotels_id"];
            $this->assign("data", $data[0]);
        }
        else{
            $hotels_id = I("hotels_id");
            if(empty($hotels_id)){
                $this->error("必须先选择一个酒店", U("Hotels/room"));
            }
        }
        $hotel = $this->M->where("id=".$hotels_id)->select();
        $hotel = $hotel[0]["name"];
        $type = M("rooms_type")->select();
        $this->assign("hotel", $hotel);
        $this->assign("hotels_id", $hotels_id);
        $this->assign("type", $type);
        $this->display();
    }
/**
*   客房信息处理过程
*/
    public function doEditRoom(){
        $room = M("rooms");
        $data = $room->create();
        if(empty($data["id"])){
            $res = $room->data($data)->add();
            // 初次添加数据时上传图片
            $picres = $this->upPhotoRoom($res);
        }
        else{
            $res = $room->data($data)->save();
            // 更新信息时上传图片
            $picres = $this->upPhotoRoom($data["id"]);
        }
        if($res || $picres){
            $this->success("客房信息已被修改，".$picres, U("Hotels/room"));
        }
        else{
            $this->error("操作失败", U("Hotels/room"));
        }
    }
/*
*   客房图片上传
*/
    public function upPhotoRoom($id = ""){
        if(empty($id)){
            return ;
        }
        // 图片上传
        $upload = new \Think\Upload();// 实例化上传类
        $upload->maxSize   =     3145728 ;// 设置附件上传大小
        $upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
        $upload->savePath  =     CONTROLLER_NAME.'/Rooms/'; // 设置附件上传目录    // 上传文件
        $upload->subName = array('date','Y/m');
        $info = $upload->upload();
        if(!$info) {// 上传错误提示错误信息
            return $upload->getError();
        }else{// 上传成功
            $pic = M("rooms_pic");
            $picture = array();
            foreach($info as $v){
                $picture = substr($v["savepath"], strlen(CONTROLLER_NAME)+7) . $v["savename"];
                $pic->data(array("rooms_id"=>$id, "picture"=>$picture))->add();
            }
            return count($info)."个图片已上传";
        }
    }
/*
*   ajax请求查询客房图片
*/
    public function getPicRoom(){
        $id = I("id");
        $picinfo["pic"] = M("rooms_pic")->where("rooms_id=".$id)->select();
        $picinfo["pics"] = M("rooms_pic")->where("rooms_id=".$id)->count();
        echo json_encode($picinfo);
    }
/*
*   ajax请求删除客房图片
*/
    public function delPicRoom(){
        $id = I("id");
        $picture = $this->M->table("__ROOMS_PIC__")->where("id=".$id)->select();
        $delres = unlink(C("TMPL_PARSE_STRING")["__UPLOAD__"]."/".CONTROLLER_NAME."/Rooms/".$picture[0]["picture"]);
        if($delres){
            $res = $this->M->table("__ROOMS_PIC__")->where("id=".$id)->delete();
        }
        echo $res;
    }
/*
*   删除客房信息
*/
    public function delRoom(){
        $id = I("id");
        $pic = M("rooms_pic");
        $res1 = M("rooms")->where("id=".$id)->delete();
        $data = $pic->where("rooms_id=".$id)->select();
        foreach($data as $v){
            unlink(C("TMPL_PARSE_STRING")["__UPLOAD__"]."/".CONTROLLER_NAME."/rooms/".$v["picture"]);
        }
        $res2 = $pic->where("rooms_id=".$id)->delete();
        if($res1){
            $this->success("操作成功", U("Hotels/room"));
        }
        else{
            $this->error("操作失败", U("Hotels/room"));
        }
    }

// ----------------分--------割--------线--------小--------盆--------友----------------

/*
*   房型列表
*/
    public function type($value=''){
        $type = M("rooms_type");
        $count = $type->count();    // 总记录数
        $pages = ceil($count / $this->perpage);
        $nowpage = I("p");
        if(empty($nowpage)){
            $nowpage = 1;
        }
        $Page = new \Think\Page($count, $this->perpage);
        $Page->setConfig("prev", "上一页");
        $Page->setConfig("next", "下一页");
        $list = $type->limit($Page->firstRow.','.$Page->listRows)->select();   // 每页的数据数组
        $show = $Page->show();  // 页码链接内容
        $this->assign("count", $count);
        $this->assign("list", $list);
        $this->assign("page", $show);
        $this->assign("pages", $pages);
        $this->assign("nowpage", $nowpage);
        $this->display();
    }
/**
*   添加/修改客房类型
*/
    public function editType(){
        $id = I("id");
        if(!empty($id)){
            $data = M("rooms_type")->where("id=".$id)->select();
            $data = $data[0];
            $this->assign("data", $data);
        }
        $this->display();
    }
/**
*   客房类型编辑修改处理程序
*/
    public function doEditType(){
        $type = M("rooms_type");
        $data = $type->create();
        if(empty($data["id"])){
            $res = $type->add();
        }
        else{
            $res = $type->save();
        }
        if($res){
            $this->success("操作成功", U("Hotels/type"));
        }
        else{
            $this->error("操作失败", U("Hotels/editType"));
        }
    }
/*
*   删除客房类型
*/
    public function delType(){
        $id = I("id");
        $res = M("rooms_type")->where("id=".$id)->delete();
        if($res){
            $this->success("操作成功", U("Hotels/type"));
        }
        else{
            $this->error("操作失败", U("Hotels/type"));
        }
    }

// ----------------分--------割--------线--------小--------盆--------友----------------

/**
*   地区联动
*/
    public function area(){
        $id = I("id");
        if(!empty($id)||$id==0){
            $area = M("area");
            $res = $area->where("reid=".$id)->select();
        }
        else{
            $res = array("empty");
        }
        echo json_encode($res);
    }
}
